home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!pladow
- From: pladow@netcom.com (Peter Ladow)
- Subject: Template woes
- Message-ID: <pladowDpztGt.Bvo@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- X-Newsreader: TIN [version 1.2 PL1]
- Date: Wed, 17 Apr 1996 06:17:16 GMT
- Sender: pladow@netcom22.netcom.com
-
- I am using Borland C++ 4.0, and I'm trying to learn how to do data
- structures in C++. I have taken a course in C (over 3 yrs ago however)
- so I understand the basics. Right now I am trying to to a binary tree
- using templates.
-
- Here is my interface (thus far):
-
- tree.h:
- #include <iostream.h>
-
- // Forward declaration
- template <class TYPE> class Tree;
-
- template <class TYPE>
- class Leaf {
- private:
- friend class Tree<TYPE>;
-
- // Data members
- Leaf<TYPE>* left;
- Leaf<TYPE>* right;
- TYPE data;
-
- // Constructor
- Leaf(TYPE d, Leaf<TYPE>* l, Leaf<TYPE>* r) : data(d), right(r), left(l){}
- };
-
- template <class TYPE>
- class Tree {
- public:
- // Constructor
- Tree(void) {root = NULL; }
-
- // Access functions
- TYPE find(TYPE d);
- void insert(TYPE d);
-
- private:
-
- // Root node
- Leaf<TYPE>* root;
-
- // Internal function
- TYPE find(Leaf<TYPE>*r, TYPE d) const;
- };
-
- Now the problem isn't during compilation. When the linker trys to resolve
- symbols I get:
- Linker Error: Undefined symbol Tree<int>::insert(int) in module TEST.CPP
-
- In my main() code I have:
-
- .
- .
- .
- Tree<int> test;
- int a=5;
-
- test.insert(a);
- .
- .
- .
-
- An interesting note is that I had my constructor for class Tree external
- and I got the same error. But when I made it inline, it went away.
-
- Here is my implementation of Test<TYPE>::insert(TYPE d);
-
- tree.cpp:
- #include <string.h>
-
- template <class TYPE>
- void Tree<TYPE>::insert(TYPE d) {
- Leaf<TYPE>* temp=root;
- Leaf<TYPE>* old;
-
- if (root == NULL) {
- root=new Leaf<TYPE>(d, NULL, NULL);
- return;
- }
-
- while(temp != NULL) {
- old=temp;
- if ((temp->data, d) == 0) {
- return;
- }
-
- if (comp(temp->data, d) > 0)
- temp=temp->left;
-
- else
- temp=temp->right;
- }
-
- if (comp(old->data, d) > 0)
- old->left=new Leaf<TYPE>(d, NULL, NULL);
- else
- old->right=new Leaf<TYPE>(d, NULL, NULL);
- }
-
- Now, while looking through the Programmers Guide that came with Borland,
- I found the example of the vector class (on page 165). When I typed
- exactly what I read there, I got the same error for Vector<T>::Vector(int n);
-
- Is this a bug? Am I typing something wrong? (Note I even tried a semicolon
- at the end of the insert() function like that in the book, but no luck. That
- is just a syntax issue though.)
-
- Note that in all these examples, the interface is separate from the
- implementation, i.e. in different source files.
-
- Thanks a lot for you help.
-
- Pete
- pladow@netcom.com
- ___________________________________________________________________________
- | | "To love for the sake of being loved is |
- |ORQ: And the meek shall | human, to love for the sake of loving |
- | inherit the earth... | is angelic." --Alphonse de Lamartine |
- ---------------------------------------------------------------------------
-